;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.Overdue", [])
        .controller("OverdueCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "Upload", "$timeout", "EventBus", "$filter", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, Upload, $timeout, EventBus, $filter) {
            $scope.markInfo = {
                allChecked: false,
                curRow: 0
            };
            $scope.activeFlag = [true, false, false];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 新增编辑tab切换
            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };
            // 新增编辑
            $scope.detailInfo = {};
            $scope.changeTab = function (index) {
            $scope.activeFlag = [false, false, false];
            $scope.activeFlag[index] = true;
            $scope.submitted = false;
                switch (index) {
                    case 0:
                        break;
                    case 1: $scope.detailInfo = {};
                        break;
                    case 2:
                        break;
                    default:
                        break;
                }
            };

            $scope.dataList = [];
            $scope.condition = {};
            $scope.btnEnabled = true;
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };
            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/dealerOverdue/export")
            };
            $scope.tableConfig = {
                fixedWidth: 120,
                scrollWidth: 1000,
                tableCol: [
                    {
                        type: "",
                        name: "经销商编码",
                        dataField: "dealerCode",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "是否逾期",
                        dataField: "overdue",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "经销商名称",
                        dataField: "companyName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 400,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            var btnPermissionCode = {
                dealerLimitSave: window.ActionCode.dealerLimitSave,
                dealerLimitImport: window.ActionCode.dealerLimitImport,
                dealerLimitExport: window.ActionCode.dealerLimitExport
            };

            $scope.btnPermission = {
                dealerLimitSave: true,
                dealerLimitImport: true,
                dealerLimitExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {left: $scope.tableConfig.fixedWidth + "px"}
                };
            };

            $scope.selectAll = function (list) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/dealerOverdue/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = $scope.condition;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    angular.forEach(resp.value.list, function (item) {
                        item.oldLimit = item.totalLimit;
                        item.isChecked = false;
                        item.overdue = item.overdue == 0 ? '否' : '是'
                    });
                    $scope.markInfo.allChecked = false;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };
            // 下载/导入
            $scope.importDate = function () {
                var param = {
                    name: "逾期经销商",
                    templateReqService: "device",
                    templateReqUrl: "/dealerOverdue/download",
                    importReqService: "device",
                    importReqUrl: "/dealerOverdue/import",
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };

            $scope.selectScheme = function () {
                $scope.btnEnabled = false;
            };

            $scope.saveData = function () {
                var service = "device";
                var reqUrl = "/dealerLimit/updateLimit";
                ybHttp.Post(service, reqUrl, $scope.dataList).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                });
            };
            //新增
            $scope.addData = function () {
                $scope.tabFlag.name = tabFlag[0];
                $scope.changeTab(1);
                $scope.activeFlag[1] = true
            };            
            // 新增数据 
            /**
             * 8/12和后端沟通编辑功能先不开发
             * 故先隐藏增加功能
             */
            $scope.saveData = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var reqUrl = "/dealerOverdue/insert";
                if ($scope.tabFlag.type === 1) {
                    reqUrl = "/dealerOverdue/update";
                }
                const param = {
                    ...angular.copy($scope.detailInfo),
                };
                
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                });
            };    
            // 编辑查询
            $scope.editData = function (item) {
                 $scope.changeTab(1);
                 $scope.tabFlag.type = 1;
                 $scope.tabFlag.name = tabFlag[1];
                 $scope.detailInfo = angular.copy(item)
                 $scope.detailInfo.updatedTime = undefined
                 $scope.detailInfo.overdue = $scope.detailInfo.overdue == '否'? '0' : '1'                 
            };
            // 批量删除
            $scope.delData = function () {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/dealerOverdue/delete";
                    var param = [];
                    // param = $scope.dataList.filter(item => item.isChecked == true)
                    $scope.dataList.forEach(item => {
                        if (item.isChecked == true) {
                            param.push(item.id)
                        }
                    });
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                }, function () {

                })
            };            
            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);